const LogPage={
    async openPage(requestDetail, responseData ,curl) {
        var win = Utils.openWindowWithFavicon();
        let title = document.title;
        // 设置基本样式
        win.document.write(`
        <head>
            <title>${title}</title>
        </head>
        <style>
            body { font-family: Arial, sans-serif; margin: 20px; }
            h2 { color: #333; }
            .my-content { background: #f9f9f9; border: 1px solid #ddd; padding: 10px; margin-bottom: 20px; white-space: pre-wrap; word-wrap: break-word;max-height: 280px;overflow-y: auto; }
            .alert { padding: 10px; border-radius: 4px; margin-bottom: 20px; }
            .alert-danger { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
            .alert-info { background-color: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
            .alert-success {background-color: #d4edda;color: #155724;border: 1px solid #c3e6cb;}
            table { width: 100%; border-collapse: collapse; margin-top: 20px; }
            th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
            th { background-color: #f2f2f2; }
        </style>
        <script>
            function openComparePage(bizId, requestMethod, businessGuid,userGuid,tenantCode){
              window.opener.openComparePage(bizId, requestMethod, businessGuid,userGuid,tenantCode)
            }
        </script>
        `);
        win.document.write(`<h3>CURL</h3><pre class="my-content" id="curl-display"></pre>`);
        win.document.getElementById('curl-display').textContent = curl ;
        win.document.write(`<h3>Response</h3><pre class="my-content">${responseData}</pre>`);
        win.document.write(`<h3>Logs</h3>`);
        if (JSON.parse(responseData).success === false) {
            win.document.write(`<div class="alert alert-danger" role="alert">接口调用失败，请检查参数！</div>`, '*');
            return;
        }
        win.document.write(`<div id="loading"></div>`);
        win.document.write(`<div id="tableArea"></div>`);
        win.document.getElementById('loading').innerHTML = `<div class="alert alert-info" role="alert">正在查询结果，请稍后...</div>`;

        let startTime = Date.now();
        let userGuid = requestDetail.requestBody.receivers[0].receiverId;
        let {
            html,
            len
        } = await LogPage.getResultHTML(requestDetail.requestBody.bizId, requestDetail.requestMethod, requestDetail.requestBody.businessGuid, userGuid,requestDetail.requestBody.tenantCode);

        win.document.getElementById('tableArea').innerHTML = html;
        let count = 0;

        if (len >= 4 && !html.includes("null")) {
            let cost = Date.now() - startTime;
            win.document.getElementById('loading').innerHTML = `<div class="alert alert-success" role="alert">Complete，查询耗时${cost}ms</div>`;
            return;
        }

        let timer = setInterval(async function () {
            count++;
            let {
                len: newLen,
                html: newHtml
            } = await LogPage.getResultHTML(requestDetail.requestBody.bizId, requestDetail.requestMethod, requestDetail.requestBody.businessGuid,userGuid,requestDetail.requestBody.tenantCode);
            win.document.getElementById('tableArea').innerHTML = newHtml;
            if (count >= 6 || newLen >= 4) {
                clearInterval(timer);
                let cost = Date.now() - startTime;
                win.document.getElementById('loading').innerHTML = `<div class="alert alert-success" role="alert">Complete，查询耗时${cost}ms</div>`;
            }
        }, 1500);
    },
    async getResultHTML(bizId, requestMethod, businessGuid,userGuid,tenantCode) {
        let result= await RequestLocal.getResult(bizId, requestMethod, businessGuid);
        let records = result.data;
        let html = "";
        if (Array.isArray(records) && records.length > 0) {
            html = '<table class="table table-bordered"><thead><tr><th>序号</th><th>步骤名称</th><th>异常信息</th><th>状态</th><th>操作</th></tr></thead><tbody>';
            records.forEach(row => {
                html += `<tr>
                    <td style="width:15%;">${row.stepRank}</td>
                    <td style="width:15%;">${row.stepName}</td>
                    <td style="width:40%;">${row.exceptionMessage}</td>
                    <td style="width:15%;">${row.status === 1 ? '成功' : '失败'}</td>
                    <td style="width:15%;">${row.stepName === '消息存储'
                    ? `<a href="javascript:;" onclick="openComparePage('${bizId}','${requestMethod}','${businessGuid}','${userGuid}','${tenantCode}')">数据对比</a>`
                    : ''
                }</td>
                </tr>`;
            });
            html += '</tbody></table>';
        } else {
            html = "无结果";
        }
        return {html, len: records.length};
    }
};